package job2;

/**
 * 主线程自旋轮询结果
 */
public class Main8 {

    private static volatile int result = -1;

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();

        // 异步执行 下面方法
        Thread thread = new Thread(() -> {
            result = sum();
        });
        thread.start();

        //自旋
        while (true) {
            if (result > 0) {
                break;
            }
            Thread.sleep(10);
        }

        // 确保  拿到result 并输出
        System.out.println("异步计算结果为：" + result);
        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if (a < 2)
            return 1;
        return fibo(a - 1) + fibo(a - 2);
    }

}
